#include <SoftwareSerial.h>
#include <dht11.h> 
#define DHT11_PIN 2 
#define BEEP 7
#define WIFI 6
dht11 DHT11;


int GAS_SENSOR_PIN = 0;
int co,tmp,hum;
int chk;
bool warning = false;
bool beepSet = false;
bool check = true;

void senddata(String tmp, String hum, String co);

void setup() {
    
  pinMode(BEEP,OUTPUT);
  pinMode(WIFI,OUTPUT);
  pinMode(DHT11_PIN,INPUT);
  
  Serial.begin(115200);
  while(!Serial)
  {
    ;
    }
  digitalWrite(WIFI,false);
}

void loop() 
{
  while(Serial.available()>0)
  {
    if(Serial.read() == 'W')
    {
      check = !check;
      digitalWrite(WIFI,check);
      }
      else
      {
        digitalWrite(WIFI,false);
        }
    }
  
  co=analogRead(GAS_SENSOR_PIN);        //gas ppm
  chk = DHT11.read(DHT11_PIN);          //将读取到的值赋给chk
  tmp=DHT11.temperature;               //将温度值赋值给tem
  hum=DHT11.humidity;                   //将湿度值赋给hum

  if(co>=400)
  {
    warning = true;
    beepSet = !beepSet;
  }
  else
  {
    warning = false;  
    beepSet = false;
  }

  digitalWrite(BEEP,beepSet);
   Serial.write('D');
   Serial.write(tmp);
   Serial.write(',');
   Serial.write(hum);
   Serial.write(',');
   Serial.write(co/4);
   Serial.write(',');

  delay(1000);

}



